﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;

namespace Yxinmt.Controls
{
    public class PopupImage : BaseControl, IControl
    {
        private System.Windows.Controls.Primitives.Popup _Popup { get; set; }
        private System.Windows.Controls.DockPanel _Panel { get; set; }

        System.Windows.Controls.Image _Image { get; set; }

        public PopupImage(Dictionary<string, object> property, IComponent component, bool isFactory = false)
        {
            Property = property;
            _Component = component;

            _Popup = new System.Windows.Controls.Primitives.Popup();
            _Panel = new System.Windows.Controls.DockPanel();
            this.Element = _Panel;
          
            InitSet();

           
            this.SetProperty();
            this.SetControlProperty(false, false);     
        }

        private void SetProperty()
        {
            Property["BackColor"] = "blue";

            _Image = new System.Windows.Controls.Image();
            _Image.Width = Property.GetValue<double>("Width");
            _Image.Height = Property.GetValue<double>("Height");
            _Image.Source = GetImage(Property.GetStringValue("ImageUrl"));

            var image = new System.Windows.Controls.Image();
            image.Width = _Image.Width;
            image.Height = _Image.Height;
            image.Source = _Image.Source;

            _Panel.Children.Add(_Image);
            _Panel.Children.Add(_Popup);

            _Popup.Width = _Image.Width;
            _Popup.Height = _Image.Width;
            _Popup.Placement = System.Windows.Controls.Primitives.PlacementMode.Relative;
            _Popup.AllowsTransparency = true;
            _Popup.PopupAnimation = System.Windows.Controls.Primitives.PopupAnimation.None;
            _Popup.PlacementTarget = _Image;
            _Popup.Child = image;
            _Popup.IsOpen = true;
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.DockPanel c = _Panel;
            switch (name)
            {
                case "name": c.Name = string.Concat("dpnl_", (string)value); SetFactoryProperty("Name", c.Name); break;
                case "Height": c.Height = Convert.ToDouble(value); SetFactoryProperty("Height", c.Height); break;
                case "Width": c.Width = Convert.ToDouble(value); SetFactoryProperty("Width", c.Width); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "BackColor": c.Background = GetColor((string)value); SetFactoryProperty("Background", c.Background); break;
                case "ControlAlign": SetControlAlign((string)value); break;
                case "Visible": SetVisible(Convert.ToBoolean(value)); break;
                case "GridRowColumn": SetGridRowColumn((string)value); break;
                case "BackImageUrl": SetBackgroundImage((string)value); break;
                case "ZIndex": SetZIndex(Convert.ToInt32(value)); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex(Convert.ToInt32(value)); break;
                case "Margin": c.Margin = this.GetThickness((string)value); SetFactoryProperty("Margin", c.Margin); break;
            }
        }

        public override void EventLoad()
        {
            _Panel.MouseDown += _Popup_MouseDown;
        }

        private void _Popup_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                Utility.MouseHook.DropMove();
                var p2 = Utility.MouseHook.GetCursorPoint();
                Utility.Win32Api.Mouse_Up(p2.X, p2.Y);
            }
        }
    }
}
